package com.dqp.cslggroup.JWXT;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.bean.Grade;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetStudentGrade {
    private String url;
    private int year;
    private int term;
    private Map<String, String> grade_cookies;
    private Connection.Response response;
    private String stuNum;
    private String Authorization = SharedPreferencesUtil.getStringSp(MyApplication.getContext(), "Authorization", "");

    GetStudentGrade(String grade_url, int year, int term, Map<String, String> cookies, String sno) {
        this.url = grade_url;
        this.year = year;
        this.term = term;
        this.grade_cookies = cookies;
        this.stuNum = sno;
    }

    public List<Grade> getGrade() throws Exception {
        List<Grade> grades = new ArrayList<>();
        Map<String, String> datas = new HashMap<>();
        datas.put("xnm", String.valueOf(year));
        datas.put("xqm", String.valueOf(term * term * 3));
        datas.put("_search", "false");
        datas.put("nd", String.valueOf(new Date().getTime()));
        datas.put("queryModel.showCount", "20");
        datas.put("queryModel.currentPage", "1");
        datas.put("queryModel.sortName", "");
        datas.put("queryModel.sortOrder", "asc");
        datas.put("queryModel.sortName", "");
        datas.put("time", "0");
        response = Jsoup.connect(url + "/jwglxt/cjcx/cjcx_cxDgXscj.html?doType=query&gnmkdm=N305005")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                .cookies(grade_cookies)
                .header("Authorization", Authorization)
                .method(Connection.Method.POST)
                .data(datas)
                .ignoreContentType(true)
                .timeout(10000)
                .execute();
        if (response.body() != null && response.body().contains("{")) {
            JSONObject jsonObject = JSON.parseObject(response.body());
            JSONArray gradeTable = JSON.parseArray(jsonObject.getString("items"));
            for (Object o : gradeTable) {
                JSONObject lesson = (JSONObject) o;
                Grade grade = new Grade();
                grade.setCname(lesson.getString("kcmc"));
                grade.setXf(lesson.getString("xf"));
                grade.setGrade(lesson.getString("bfzcj"));
                grade.setJd(lesson.getString("jd"));
                grade.setJxb_id(lesson.getString("jxb_id"));
                grade.setXnm(lesson.getString("xnm"));
                grade.setXqm(lesson.getString("xqm"));
                grades.add(grade);
            }
            return grades;
        }
        return null;
    }

    String getGradeMore(String jxb_id, String xnm, String xqm, String kcmc) throws Exception {
        response = Jsoup.connect(url + "/jwglxt/cjcx/cjcx_cxCjxq.html?time=" + new Date().getTime() + "&gnmkdm=N305005")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                .cookies(grade_cookies)
                .timeout(10000)
                .header("Authorization", Authorization)
                .method(Connection.Method.POST)
                .data("jxb_id", jxb_id)
                .data("xnm", xnm)
                .data("xqm", xqm)
                .data("kcmc", kcmc)
                .ignoreContentType(true)
                .execute();
        Document document = Jsoup.parse(response.body());
        return document.select("tbody").select("tr").text();
    }

}
